using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Zune.Forms
{
    /// <summary>
    /// An onscreen Keyboard.
    /// </summary>
    public class Keyboard : Control
    {
        #region Internal Events/Delegates
        internal delegate void KeyboardEventHandler(object sender, Keys key);
        internal event KeyboardEventHandler KeyDown;
        internal event KeyboardEventHandler KeyUp;
        internal event EventHandler KeyboardClosed;
        #endregion

        #region Internal Enums
        internal enum KeyboardLayouts
        {
            Alphabet,
            Other
        }
        #endregion

        #region Private/Internal Members
        internal Texture2D _keyUpTexture;
        internal Texture2D _keyDownTexture;
        internal Texture2D _spaceBar;
        internal Texture2D _spaceBarPressed;
        internal Texture2D _spaceBarHLine;
        internal Texture2D _otherKeyTexture;
        internal Texture2D _otherKeyPressedTexture;
        internal Texture2D _otherKeyHLine;
        internal Texture2D _vLine;
        internal Texture2D _hLine;
        internal Texture2D _enterTexture;
        internal Texture2D _enterTexturePressed;
        internal Texture2D _shiftTexture;
        internal Texture2D _shiftTexturePressed;
        internal Texture2D _doneTexture;
        internal Texture2D _doneTexturePressed;
        internal Texture2D _behindTextBox;
        internal Color _downForeColor = Color.Black;
        internal Color _borderColor = Color.White;
        internal bool _isInPosition = false;
        internal SpriteFont _font;
        internal SpriteFont _fontDown;
        internal List<KeyboardKey> _keyboard1Keys = new List<KeyboardKey>();
        internal List<KeyboardKey> _keyboard2Keys = new List<KeyboardKey>();
        internal KeyboardLayouts _layout = KeyboardLayouts.Alphabet;
        internal KeyboardKey _lastClicked;
        internal KeyboardKey _lastUp;
        internal bool _shiftClicked = false;
        internal TextBox _textBox;
        internal Button _backspace;
        internal TextBox _textControl;
        #endregion

        #region Constructor
        internal Keyboard(Form form, bool isSystem) : base(form, isSystem)
        {
            _location = new Vector2(1, 280);
            _size = new Vector2(270, 200);
            _textBox = new TextBox(this);
            _textBox._location = new Vector2(_location.X, _location.Y);
            _textBox._size = new Vector2(242, 40);
            _textBox.BackColor = Color.White;
            _textBox.ForeColor = Color.Black;
            _textBox.Visible = false;
            _textBox.TextChanged += new EventHandler(_textBox_TextChanged);
            _backspace = new Button(this);
            _backspace._location = new Vector2(_textBox.Location.X + _textBox.Size.X, _location.Y);
            _backspace._size = new Vector2(30, 40);
            _backspace.BackgroundImage = "Backspace";
            _backspace.BackgroundImagePressed = "BackspacePressed";
            _backspace.Visible = false;
            _backspace.Text = string.Empty;
            _backspace.Clicked += new OnControlTouched(_backspace_Clicked);
            //_location = new Vector2(0, 480);
            _backColor = Color.White;
        }
        #endregion

        #region Overrides
        public override void LoadContent()
        {
            _keyUpTexture = Drawing.CreateRectangle(GraphicsDevice, 27, 40, Color.Black);
            //_keyDownTexture = Drawing.CreateRectangle(GraphicsDevice, 27, 75, Color.White);
            _keyDownTexture = _form.Content.Load<Texture2D>("KeyDown");
            _spaceBar = Drawing.CreateRectangle(GraphicsDevice, 162, 40, Color.Black);
            _spaceBarPressed = Drawing.CreateRectangle(GraphicsDevice, 162, 75, Color.White);
            _spaceBarHLine = Drawing.CreateLine(GraphicsDevice, 0, 0, 162, 1, Color.White);
            _hLine = Drawing.CreateLine(GraphicsDevice, 0, 0, 40, 1, Color.White);
            _vLine = Drawing.CreateLine(GraphicsDevice, 0, 0, 1, 40, Color.White);
            _otherKeyTexture = Drawing.CreateRectangle(GraphicsDevice, 54, 40, Color.Black);
            _otherKeyPressedTexture = Drawing.CreateRectangle(GraphicsDevice, 54, 75, Color.White);
            _otherKeyHLine = Drawing.CreateLine(GraphicsDevice, 0, 0, 54, 1, Color.White);
            _enterTexture = _form.Content.Load<Texture2D>("EnterUp");
            _enterTexturePressed = _form.Content.Load<Texture2D>("EnterPressed");
            _shiftTexture = _form.Content.Load<Texture2D>("Shift");
            _shiftTexturePressed = _form.Content.Load<Texture2D>("ShiftPressed");
            _doneTexture = _form.Content.Load<Texture2D>("Done");
            _doneTexturePressed = _form.Content.Load<Texture2D>("DonePressed");
            _font = _form.Content.Load<SpriteFont>("Keyboard");
            _fontDown = _form.Content.Load<SpriteFont>("KeyboardDown");
            _behindTextBox = Drawing.CreateRectangle(GraphicsDevice, (Int32)_backspace.Size.X, (Int32)_backspace.Size.Y, Color.Black);
            
            LoadKeyboard1();
            LoadKeyboard2();

            base.LoadContent();
        }
        #endregion

        #region Internal Methods
        internal void LoadKeyboard2()
        {
            _keyboard2Keys.Clear();
            Vector2 keyLocation = new Vector2(_location.X, _location.Y + _textBox._size.Y);

            //Row 1:
            List<Keys> row1 = Create2Row(1);
            foreach (Keys key in row1)
            {
                KeyboardKey keyboardKey = new KeyboardKey(this);
                keyboardKey.KeyDown += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyDown);
                keyboardKey.KeyUp += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyUp);
                keyboardKey.SetKey(key, _keyUpTexture, _keyDownTexture, _vLine, _hLine);
                keyboardKey._location = keyLocation;
                keyboardKey._size = new Vector2(_keyDownTexture.Width, _keyDownTexture.Height);
                _keyboard2Keys.Add(keyboardKey);
                keyLocation = new Vector2(keyLocation.X + _keyUpTexture.Width, keyLocation.Y);
            }

            //Row 2:
            keyLocation = new Vector2(_location.X, keyLocation.Y + _keyUpTexture.Height);
            List<Keys> row2 = Create2Row(2);
            foreach (Keys key in row2)
            {
                KeyboardKey keyboardKey = new KeyboardKey(this);
                keyboardKey.KeyDown += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyDown);
                keyboardKey.KeyUp += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyUp);
                keyboardKey.SetKey(key, _keyUpTexture, _keyDownTexture, _vLine, _hLine);
                keyboardKey._location = keyLocation;
                keyboardKey._size = new Vector2(_keyDownTexture.Width, _keyDownTexture.Height);
                _keyboard2Keys.Add(keyboardKey);
                keyLocation = new Vector2(keyLocation.X + _keyUpTexture.Width, keyLocation.Y);
            }

            //Row 3:
            keyLocation = new Vector2(_location.X, keyLocation.Y + _keyUpTexture.Height);
            List<Keys> row3 = Create2Row(3);
            foreach (Keys key in row3)
            {
                KeyboardKey keyboardKey = new KeyboardKey(this);
                keyboardKey.KeyDown += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyDown);
                keyboardKey.KeyUp += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyUp);
                keyboardKey.SetKey(key, _keyUpTexture, _keyDownTexture, _vLine, _hLine);
                keyboardKey._location = keyLocation;
                keyboardKey._size = new Vector2(_keyDownTexture.Width, _keyDownTexture.Height);
                _keyboard2Keys.Add(keyboardKey);
                keyLocation = new Vector2(keyLocation.X + _keyUpTexture.Width, keyLocation.Y);
            }

            keyLocation = new Vector2(_location.X, keyLocation.Y + _keyUpTexture.Height);
            KeyboardKey otherKey = new KeyboardKey(this);
            otherKey.KeyDown += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyDown);
            otherKey.KeyUp += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyUp);
            otherKey.SetKey(Keys.Help, _otherKeyTexture, _otherKeyPressedTexture, _vLine, _otherKeyHLine);
            otherKey._location = keyLocation;
            otherKey._size = new Vector2(_otherKeyTexture.Width, _otherKeyTexture.Height);
            _keyboard2Keys.Add(otherKey);

            keyLocation = new Vector2(keyLocation.X + otherKey._size.X, keyLocation.Y);
            KeyboardKey spaceKey = new KeyboardKey(this);
            spaceKey.KeyDown += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyDown);
            spaceKey.KeyUp += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyUp);
            spaceKey.SetKey(Keys.Space, _spaceBar, _spaceBarPressed, _vLine, _spaceBarHLine);
            spaceKey._location = keyLocation;
            spaceKey._size = new Vector2(_spaceBar.Width, _spaceBar.Height);
            _keyboard2Keys.Add(spaceKey);

            keyLocation = new Vector2(keyLocation.X + spaceKey._size.X, keyLocation.Y);
            KeyboardKey doneKey = new KeyboardKey(this);
            doneKey.KeyUp += new KeyboardKey.KeyboardEventHandler(doneKey_KeyUp);
            doneKey.SetKey(Keys.Escape, _doneTexture, _doneTexturePressed, _vLine, _otherKeyHLine);
            doneKey._location = keyLocation;
            doneKey._size = new Vector2(_doneTexture.Width, _doneTexture.Height);
            _keyboard2Keys.Add(doneKey);
        }

        internal void LoadKeyboard1()
        {
            _keyboard1Keys.Clear();
            Vector2 keyLocation = new Vector2(_location.X, _location.Y + _textBox._size.Y);
            
            //Row 1:
            List<Keys> row1 = CreateRow(1);
            foreach (Keys key in row1)
            {
                KeyboardKey keyboardKey = new KeyboardKey(this);
                keyboardKey.KeyDown += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyDown);
                keyboardKey.KeyUp += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyUp);
                keyboardKey.SetKey(key, _keyUpTexture, _keyDownTexture, _vLine, _hLine);
                keyboardKey._location = keyLocation;
                keyboardKey._size = new Vector2(_keyDownTexture.Width, _keyDownTexture.Height);
                _keyboard1Keys.Add(keyboardKey);
                keyLocation = new Vector2(keyLocation.X + _keyUpTexture.Width, keyLocation.Y);
            }

            //Row 2:
            keyLocation = new Vector2(_location.X, keyLocation.Y + _keyUpTexture.Height);
            List<Keys> row2 = CreateRow(2);
            foreach (Keys key in row2)
            {
                KeyboardKey keyboardKey = new KeyboardKey(this);
                keyboardKey.KeyDown += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyDown);
                keyboardKey.KeyUp += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyUp);
                keyboardKey.SetKey(key, _keyUpTexture, _keyDownTexture, _vLine, _hLine);
                keyboardKey._location = keyLocation;
                keyboardKey._size = new Vector2(_keyDownTexture.Width, _keyDownTexture.Height);
                _keyboard1Keys.Add(keyboardKey);
                keyLocation = new Vector2(keyLocation.X + _keyUpTexture.Width, keyLocation.Y);
            }

            keyLocation = new Vector2(_location.X, keyLocation.Y + _keyUpTexture.Height);
            KeyboardKey shiftKey = new KeyboardKey(this);
            shiftKey.KeyDown += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyDown);
            shiftKey.KeyUp += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyUp);
            shiftKey.SetKey(Keys.LeftShift, _shiftTexture, _shiftTexturePressed, _vLine, _hLine);
            shiftKey._location = keyLocation;
            shiftKey._size = new Vector2(_shiftTexture.Width, _shiftTexture.Height);
            _keyboard1Keys.Add(shiftKey);
            keyLocation = new Vector2(keyLocation.X + _keyUpTexture.Width, keyLocation.Y);
            
            List<Keys> row3 = CreateRow(3);
            foreach (Keys key in row3)
            {
                KeyboardKey keyboardKey = new KeyboardKey(this);
                keyboardKey.KeyDown += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyDown);
                keyboardKey.KeyUp += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyUp);
                keyboardKey.SetKey(key, _keyUpTexture, _keyDownTexture, _vLine, _hLine);
                keyboardKey._location = keyLocation;
                keyboardKey._size = new Vector2(_keyDownTexture.Width, _keyDownTexture.Height);
                _keyboard1Keys.Add(keyboardKey);
                keyLocation = new Vector2(keyLocation.X + _keyUpTexture.Width, keyLocation.Y);
            }

            //KeyboardKey enterKey = new KeyboardKey(this);
            //enterKey.KeyDown += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyDown);
            //enterKey.KeyUp += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyUp);
            //enterKey.SetKey(Keys.Enter, _enterTexture, _enterTexturePressed, _vLine, _otherKeyHLine);
            //enterKey._location = keyLocation;
            //enterKey._size = new Vector2(_enterTexture.Width, _enterTexture.Height);
            //_keyboard1Keys.Add(enterKey);

            keyLocation = new Vector2(_location.X, keyLocation.Y + _keyUpTexture.Height);
            KeyboardKey otherKey = new KeyboardKey(this);
            otherKey.KeyDown += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyDown);
            otherKey.KeyUp += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyUp);
            otherKey.SetKey(Keys.Home, _otherKeyTexture, _otherKeyPressedTexture, _vLine, _otherKeyHLine);
            otherKey._location = keyLocation;
            otherKey._size = new Vector2(_otherKeyTexture.Width, _otherKeyTexture.Height);
            _keyboard1Keys.Add(otherKey);

            keyLocation = new Vector2(keyLocation.X + otherKey._size.X, keyLocation.Y);
            KeyboardKey spaceKey = new KeyboardKey(this);
            spaceKey.KeyDown += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyDown);
            spaceKey.KeyUp += new KeyboardKey.KeyboardEventHandler(keyboardKey_KeyUp);
            spaceKey.SetKey(Keys.Space, _spaceBar, _spaceBarPressed, _vLine, _spaceBarHLine);
            spaceKey._location = keyLocation;
            spaceKey._size = new Vector2(_spaceBar.Width, _spaceBar.Height);
            _keyboard1Keys.Add(spaceKey);

            keyLocation = new Vector2(keyLocation.X + spaceKey._size.X, keyLocation.Y);
            KeyboardKey doneKey = new KeyboardKey(this);
            doneKey.KeyUp += new KeyboardKey.KeyboardEventHandler(doneKey_KeyUp);
            doneKey.SetKey(Keys.Escape, _doneTexture, _doneTexturePressed, _vLine, _otherKeyHLine);
            doneKey._location = keyLocation;
            doneKey._size = new Vector2(_doneTexture.Width, _doneTexture.Height);
            _keyboard1Keys.Add(doneKey);
        }

        internal void ShowKeyboard(TextBox textBox)
        {
            _textControl = textBox;
            _textBox.TextMode = textBox.TextMode;
            if (_textBox.TextMode == Enumerations.TextModes.Multiline && _textBox.Size.Y == 40)
            {
                _location = new Vector2(_location.X, _location.Y - _textBox.Size.Y);
                _size = new Vector2(_size.X, _size.Y + _textBox.Size.Y);
                _textBox.Size = new Vector2(_textBox.Size.X, _textBox.Size.Y * 2);
                _textBox.Location = new Vector2(_textBox.Location.X, _location.Y);
                LoadKeyboard1();
                LoadKeyboard2();
            }
            else if (_textBox.Size.Y == 80)
            {
                _location = new Vector2(1, 280);
                _size = new Vector2(270, 200);
                _textBox._location = new Vector2(_location.X, _location.Y);
                _textBox._size = new Vector2(242, 40);
                LoadKeyboard1();
                LoadKeyboard2();
            }
            _textBox.Text = _textControl.Text;
            _textBox.SetCursorLocationByText();
            base.Visible = true;
        }

        internal void HideKeyboard()
        {
            _textControl = null;
            _textBox.Text = string.Empty;
            _textBox.SetCursorLocationByText();
            base.Visible = false;
            if (KeyboardClosed != null)
                KeyboardClosed(this, EventArgs.Empty);
            //_location = new Vector2(1, 480);
        }

        internal List<Keys> CreateRow(int rowId)
        {
            List<Keys> row = new List<Keys>();
            switch (rowId)
            {
                case 1:
                    row.Add(Keys.Q);
                    row.Add(Keys.W);
                    row.Add(Keys.E);
                    row.Add(Keys.R);
                    row.Add(Keys.T);
                    row.Add(Keys.Y);
                    row.Add(Keys.U);
                    row.Add(Keys.I);
                    row.Add(Keys.O);
                    row.Add(Keys.P);
                    break;
                case 2:
                    row.Add(Keys.A);
                    row.Add(Keys.S);
                    row.Add(Keys.D);
                    row.Add(Keys.F);
                    row.Add(Keys.G);
                    row.Add(Keys.H);
                    row.Add(Keys.J);
                    row.Add(Keys.K);
                    row.Add(Keys.L);
                    row.Add(Keys.Apps);
                    break;
                case 3:
                    row.Add(Keys.Z);
                    row.Add(Keys.X);
                    row.Add(Keys.C);
                    row.Add(Keys.V);
                    row.Add(Keys.B);
                    row.Add(Keys.N);
                    row.Add(Keys.M);
                    row.Add(Keys.OemComma);
                    row.Add(Keys.OemPeriod);
                    break;
            }
            return row;
        }

        internal List<Keys> Create2Row(int rowId)
        {
            List<Keys> row = new List<Keys>();
            switch (rowId)
            {
                case 1:
                    row.Add(Keys.NumPad1);
                    row.Add(Keys.NumPad2);
                    row.Add(Keys.NumPad3);
                    row.Add(Keys.NumPad4);
                    row.Add(Keys.NumPad5);
                    row.Add(Keys.NumPad6);
                    row.Add(Keys.NumPad7);
                    row.Add(Keys.NumPad8);
                    row.Add(Keys.NumPad9);
                    row.Add(Keys.NumPad0);
                    break;
                case 2:
                    row.Add(Keys.D1);
                    row.Add(Keys.D2);
                    row.Add(Keys.D3);
                    row.Add(Keys.D4);
                    row.Add(Keys.D5);
                    row.Add(Keys.D6);
                    row.Add(Keys.D7);
                    row.Add(Keys.D8);
                    row.Add(Keys.D9);
                    row.Add(Keys.D0);
                    break;
                case 3:
                    row.Add(Keys.F1);
                    row.Add(Keys.F2);
                    row.Add(Keys.F3);
                    row.Add(Keys.F4);
                    row.Add(Keys.F5);
                    row.Add(Keys.F6);
                    row.Add(Keys.F7);
                    row.Add(Keys.F8);
                    row.Add(Keys.F9);
                    row.Add(Keys.F10);
                    break;
            }
            return row;
        }
        #endregion

        #region EventHandlers
        protected void _textBox_TextChanged(object sender, EventArgs e)
        {
            if (_textControl != null)
                _textControl.Text = _textBox.Text;
        }

        protected void _backspace_Clicked(object sender, GameTime gameTime, TouchLocation touch)
        {
            _textBox.BackspaceClicked();
        }

        protected void doneKey_KeyUp(object sender, Keys key)
        {
            if (_textBox.Text != null)
                _textControl.Text = _textBox.Text;

            HideKeyboard();
        }

        protected void keyboardKey_KeyUp(object sender, Keys key)
        {
            KeyboardKey keyboardKey = (KeyboardKey)sender;
            if (key.ToString().StartsWith("Num") || key == Keys.D1 || key == Keys.D2 || key == Keys.D3 || key == Keys.D4 || key == Keys.D5 || key == Keys.D6
                || key == Keys.D7 || key == Keys.D8 || key == Keys.D9 || key == Keys.D0 || key == Keys.F1 || key == Keys.F2 || key == Keys.F3 || key == Keys.F4
                 || key == Keys.F5 || key == Keys.F6 || key == Keys.F7 || key == Keys.F8 || key == Keys.F9 || key == Keys.F10 || key == Keys.OemComma || key == Keys.OemPeriod)
            {
                _textBox.KeyboardClicked(keyboardKey._keyText);
            }
            else if (key != Keys.Enter && key != Keys.LeftShift && key != Keys.Home && key != Keys.Space && key != Keys.Apps && key != Keys.Help)
            {
                if (_shiftClicked)
                    _textBox.KeyboardClicked(key.ToString().ToUpper());
                else
                    _textBox.KeyboardClicked(key.ToString().ToLower());
            }
            else if (key == Keys.Space)
            {
                _textBox.KeyboardClicked(" ");
            }
            else if (key == Keys.Apps)
            {
                _textBox.KeyboardClicked("'");
            }
            else if (key == Keys.Home || key == Keys.Help)
            {
                if (_layout == KeyboardLayouts.Alphabet)
                    _layout = KeyboardLayouts.Other;
                else
                    _layout = KeyboardLayouts.Alphabet;
            }
            else if (key == Keys.Enter)
            {
                _textBox.KeyboardClicked(Environment.NewLine);
            }

            if (key == Keys.LeftShift)
            {
                if (_shiftClicked)
                    _shiftClicked = false;
                else
                    _shiftClicked = true;
            }
            else
            {
                if (_shiftClicked)
                    _shiftClicked = false;
            }
        }

        protected void keyboardKey_KeyDown(object sender, Keys key)
        {
            if (_lastClicked != null)
            {
                if ((KeyboardKey)sender != _lastClicked)
                {
                    _lastClicked._isPressed = false;
                }
            }
            _lastClicked = (KeyboardKey)sender;
        }
        #endregion

        #region Overrides
        public override void Draw(GameTime gameTime)
        {
            if (_visible)
            {
                base.Draw(gameTime);
                //if (_location.Y > 320)
                //{
                //    _location.Y -= 20;
                //}
                //else
                //{
                //   _location.Y = 320;
                   _isInPosition = true;
                //}

                   if (_textBox.TextMode == Enumerations.TextModes.Multiline)
                   {
                       SpriteBatch.Draw(_behindTextBox, new Vector2(272 - 29, _location.Y), Color.White);
                   }

                

                switch (_layout)
                {
                    case KeyboardLayouts.Alphabet:
                        foreach (KeyboardKey key in _keyboard1Keys)
                        {
                            key.Draw(gameTime);
                        }
                        break;
                    case KeyboardLayouts.Other:
                        foreach (KeyboardKey key in _keyboard2Keys)
                        {
                            key.Draw(gameTime);
                        }
                        break;
                }
                
            }
        }

        public override void Update(GameTime gameTime, TouchCollection touches)
        {
            if (touches.Count > 0)
            {
                TouchLocation touch = touches[0];
                if (Utilities.IsControlTouched(touch.Position, _location, _size))
                {
                    switch (touch.State)
                    {
                        case TouchLocationState.Moved:
                        case TouchLocationState.Pressed:
                        case TouchLocationState.Released:
                            if (_isInPosition)
                            {
                                switch(_layout)
                                {
                                    case KeyboardLayouts.Alphabet:
                                        foreach (KeyboardKey key in _keyboard1Keys)
                                        {
                                            key.Update(gameTime, touches);
                                        }
                                        break;
                                    case KeyboardLayouts.Other:
                                        foreach (KeyboardKey key in _keyboard2Keys)
                                        {
                                            key.Update(gameTime, touches);
                                        }
                                        break;
                            }
                            }
                            break;
                }
                }
                else
                {
                    //HideKeyboard();
                }
            }

            base.Update(gameTime, touches);
        }
        #endregion
    }
}